package birdsnail.springcloud.myselfserviceprovider.controller;

import birdsnail.springcloud.myselfserviceprovider.mapper.EmployeesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/v1/hello/")
public class HelloController {

    private final Map<Integer, String> userMap = new HashMap<>();

    private EmployeesMapper employeesMapper;

    @Autowired
    public void setEmployeesMapper(EmployeesMapper employeesMapper) {
        this.employeesMapper = employeesMapper;
    }

    @PostConstruct
    public void initAllUser() {
        userMap.put(1, "张三");
        userMap.put(2, "惊奇队长");
        userMap.put(3, "bear");
        userMap.put(4, "奥特曼");
        System.out.println("========================");
    }

    @GetMapping("/user/{id}")
    public String hello(@PathVariable("id") Integer id) {
        System.out.println(employeesMapper.selectByPrimaryKey(10002L));
        return getUserById(id);
    }

    private String getUserById(Integer userId) {
        return userMap.getOrDefault(userId, "自由自在的孩子");
    }
}
